<?php

namespace Acme\StaffBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Acme\StaffBundle\Entity\Staff
 *
 * @ORM\Table(name="staff")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Staff {

  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="IDENTITY")
   */
  private $id;

  /**
   * @var string $name
   *
   * @ORM\Column(name="name", type="string", length=50, nullable=false)
   */
  private $name;

  /**
   * @var text $description
   *
   * @ORM\Column(name="description", type="text", nullable=false)
   */
  private $description;

  /**
   * @var  $firm
   *
   * @ORM\ManyToOne(targetEntity="Acme\FirmsBundle\Entity\Firms", inversedBy="staffs")
   * @ORM\JoinColumn(name="firm_id", referencedColumnName="id")
   */
  private $firm;

  /**
   * @var CategoryStaff
   *
   * @ORM\ManyToOne(targetEntity="CategoryStaff")
   * @ORM\JoinColumns({
   *   @ORM\JoinColumn(name="category_id", referencedColumnName="id")
   * })
   */
  private $category;

  /**
   * @var string $path
   * 
   * @ORM\Column(name="path", type="string", length=255, nullable=true)
   */
  private $path;

  /**
   * @Assert\File(maxSize="6000000")
   */
  public $file;

  /**
   *
   * @var Comment
   * @ORM\OneToMany(targetEntity="Comment", mappedBy="staff") 
   */
  private $comments;

  /**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
  public function preUpload() {
    if (null !== $this->file) {
      // do whatever you want to generate a unique name
      $this->path = uniqid() . '.' . $this->file->guessExtension();
    }
  }

  /**
   * @ORM\PostPersist()
   * @ORM\PostUpdate()
   */
  public function upload() {
    if (null === $this->file) {
      return;
    }

    // you must throw an exception here if the file cannot be moved
    // so that the entity is not persisted to the database
    // which the UploadedFile move() method does automatically
    $this->file->move($this->getUploadRootDir(), $this->path);

    unset($this->file);
  }

  /**
   * @ORM\PostRemove()
   */
  public function removeUpload() {
    if ($file = $this->getAbsolutePath()) {
      unlink($file);
    }
  }

  /**
   * Get id
   *
   * @return integer 
   */
  public function getId() {
    return $this->id;
  }

  /**
   * Set name
   *
   * @param string $name
   */
  public function setName($name) {
    $this->name = $name;
  }

  /**
   * Get name
   *
   * @return string 
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Set description
   *
   * @param text $description
   */
  public function setDescription($description) {
    $this->description = $description;
  }

  /**
   * Get description
   *
   * @return text 
   */
  public function getDescription() {
    return $this->description;
  }

  /**
   * Set firm
   *
   * @param Acme\FirmsBundle\Entity\Firms $firm
   */
  public function setFirm(\Acme\FirmsBundle\Entity\Firms $firm) {
    $this->firm = $firm;
  }

  /**
   * Get firm
   *
   * @return Acme\FirmsBundle\Entity\Firms 
   */
  public function getFirm() {
    return $this->firm;
  }

  /**
   * Set category
   *
   * @param Acme\StaffBundle\Entity\CategoryStaff $category
   */
  public function setCategory(\Acme\StaffBundle\Entity\CategoryStaff $category) {
    $this->category = $category;
  }

  /**
   * Get category
   *
   * @return Acme\StaffBundle\Entity\CategoryStaff 
   */
  public function getCategory() {
    return $this->category;
  }

  /**
   * Set path
   *
   * @param string $path
   */
  public function setPath($path) {
    $this->path = $path;
  }

  /**
   * Get path
   *
   * @return string 
   */
  public function getPath() {
    return $this->path;
  }

  public function getAbsolutePath() {
    return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
  }

  public function getWebPath() {
    return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
  }

  public function setFile($v) {
    $this->file = $v;
    $this->removeUpload();
    $this->setPath('');
  }

  protected function getUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/' . $this->getUploadDir();
  }

  protected function getUploadDir() {
    return '/uploads/staffs';
  }

  public function __construct() {
    $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
  }

  /**
   * Add comments
   *
   * @param Acme\StaffBundle\Entity\Comment $comments
   */
  public function addComment(\Acme\StaffBundle\Entity\Comment $comments) {
    $this->comments[] = $comments;
  }

  /**
   * Get comments
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getComments() {
    return $this->comments;
  }

}